package code.rocky.chapter02.item2;

/**
 * Telescoping constructor pattern - does not scale well!
 */
public class NutritionFactsConstructor {

    private final int servingSize   ; //(mL)           required

    private final int servings      ; //(per contains) required

    private final int calories      ; // (per serving) optional
    private final int fat           ; // (g/serving)   optional
    private final int sodium        ; // (mg/serving)  optional
    private final int carbohydrate  ; // (g/serving)   optional

    public NutritionFactsConstructor(int servingSize, int servings) {
        this(servingSize,servings,0);
    }

    public NutritionFactsConstructor(int servingSize, int servings, int calories) {
        this(servingSize,servings,calories,0);
    }

    public NutritionFactsConstructor(int servingSize, int servings, int calories, int fat) {
        this(servingSize,servings,calories,fat,0);
    }

    public NutritionFactsConstructor(int servingSize, int servings, int calories, int fat, int sodium) {
        this(servingSize,servings,calories,fat,sodium,0);
    }

    public NutritionFactsConstructor(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
        this.servingSize = servingSize;
        this.servings = servings;
        this.calories = calories;
        this.fat = fat;
        this.sodium = sodium;
        this.carbohydrate = carbohydrate;
    }

    @Override
    public String toString() {
        return "NutritionFactsConstructor = {" +
                "servingSize=" + servingSize +
                ", servings=" + servings +
                ", calories=" + calories +
                ", fat=" + fat +
                ", sodium=" + sodium +
                ", carbohydrate=" + carbohydrate +
                '}';
    }

    public static void main(String[] args) {
        // 重叠构造器模式可行,但是当有许多参数的时候,客户端代码会很难编写,并且仍然较难以阅读
        NutritionFactsConstructor cocaCola =
                new NutritionFactsConstructor(240, 8, 100, 0, 35, 27);
        System.out.println(cocaCola);
    }
}
